//
//  JMyCollectController.m
//  HeadIndustry
//
//  Created by jun on 2017/9/13.
//  Copyright © 2017年 jun. All rights reserved.
//

#import "JMyCollectController.h"
#import "JLikeView.h" // 收藏宝贝

#import "JProductDetailController.h" // 商品详情
#import "JMyCollectView.h" // 收藏商店
#define BtnTag 50

@interface JMyCollectController ()<UIScrollViewDelegate, JMyCollectViewDelegate>

@property (nonatomic, strong) JLikeView *collectVeiw;
@property (nonatomic, strong) JMyCollectView *collectShopVeiw;

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, assign) CGFloat btnWidth;
@property (nonatomic, assign) NSInteger lastBtnSel;

@property (nonatomic, strong) NSMutableArray *dataEmptyViewArr;
@property (nonatomic, copy) NSArray *productDataArr;
@property (nonatomic, copy) NSArray *stopDataArr;

@end

@implementation JMyCollectController

- (void)viewDidLoad {
    [super viewDidLoad];
    [super InitUI];
    self.titleLabel.text = @"我的收藏";
    self.leftButton.hidden = NO;
    self.rightButton. hidden = YES;
    [self.leftButton setImage:[UIImage imageNamed:backBtnImg] forState:UIControlStateNormal];
    self.leftButton.frame = CGRectMake(8, self.leftButton.frame.origin.y, 30, 30);
    

    [self loadmyCollectProductRequest]; // 收藏宝贝
    [self loadMyCollectShopRequest]; // 收藏商店
    
    [self configTopView];
    
    [self setConfigScrollView];
}

- (void)setupCartEmptyView:(UIScrollView *)scrollView rect:(CGRect)rect{
    //默认视图背景
    UIView *backgroundView = [[UIView alloc]init];
    //    backgroundView.frame = CGRectMake(0, 0, kWindowW, CGRectGetHeight(self.contentView.frame));
    backgroundView.frame = rect;
    [scrollView addSubview:backgroundView];
    backgroundView.hidden = YES;
    [self.dataEmptyViewArr addObject:backgroundView];
    //默认图片
    UIImageView *img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"order_empty"]];
    img.center = CGPointMake(kWindowW/2.0, kWindowH/2.0 - 120);
    img.bounds = CGRectMake(0, 0, 100, 100);
    [backgroundView addSubview:img];
    
    UILabel *warnLabel = [[UILabel alloc]init];
    warnLabel.center = CGPointMake(kWindowW/2.0, kWindowH/2.0 - 10);
    warnLabel.bounds = CGRectMake(0, 0, kWindowW, 60);
    warnLabel.textAlignment = NSTextAlignmentCenter;
    warnLabel.text = @"您还没有相关收藏";
    warnLabel.numberOfLines = 0;
    warnLabel.font = [UIFont systemFontOfSize:16];
    warnLabel.textColor = kRGBColor(150, 150 , 150);
    [backgroundView addSubview:warnLabel];
}

- (void)configTopView {
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kWindowW, 50)];
    bgView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:bgView];
    
    NSArray *arrBtn = @[@"宝贝",@"商铺"];
    _btnWidth = kWindowW /2.0;
    for (int i = 0; i <arrBtn.count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(_btnWidth * i, 0, _btnWidth, bgView.height);
        [btn setTitle:arrBtn[i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(orderBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        btn.titleLabel.font = kFONT_TITLE(16);
        btn.tag = BtnTag + i;
        [bgView addSubview:btn];
    }
    
    CALayer *lineLayer = [CALayer layer];
    lineLayer.backgroundColor = kRGBColor(150, 150, 150).CGColor;
    lineLayer.frame = CGRectMake(0, bgView.height - 0.5, kWindowW, 0.5);
    [bgView.layer addSublayer:lineLayer];
    
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(60, bgView.height - 2, _btnWidth - 60 * 2, 2)];
    lineView.backgroundColor = [UIColor redColor];
    [bgView addSubview:lineView];
    _lineView = lineView;
}

- (void)setConfigScrollView {
    self.automaticallyAdjustsScrollViewInsets = NO;
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 50, kWindowW, CGRectGetHeight(self.contentView.frame) - 50)];
    [self.contentView addSubview:_scrollView];
    _scrollView.contentSize = CGSizeMake(kWindowW * 2, _scrollView.height);
    _scrollView.delegate = self;
    _scrollView.pagingEnabled = YES;
    _scrollView.bounces = NO;
    
    _collectVeiw = [[JLikeView alloc]initWithFrame:CGRectMake(0, -60, kWindowW, _scrollView.height + 60) isHeadView:NO];
    [_scrollView addSubview:_collectVeiw];
    
    __weak typeof(self) weakSelf = self;
    
    _collectVeiw.cellSelectBlock = ^(JProductDetailModel *selModel) {
        JProductDetailController *detail = [[JProductDetailController alloc]init];
        detail.productModel = selModel;
        detail.inputType = 1; // 表示非购物车进入商品详情
        [weakSelf.navigationController pushViewController:detail animated:YES];
    };
    
    _collectShopVeiw = [[JMyCollectView alloc]initWithFrame:CGRectMake(kWindowW, 0, kWindowW, _scrollView.height)];
    _collectShopVeiw.delegate = self;
    [_scrollView addSubview:_collectShopVeiw];
    
    CGRect rect = CGRectMake(0, 0, kWindowW, _scrollView.height);
    [self setupCartEmptyView:_scrollView rect:rect];
    
    CGRect rect1 = CGRectMake(kWindowW, 0, kWindowW, _scrollView.height);
    [self setupCartEmptyView:_scrollView rect:rect1];
    
}

- (void)loadmyCollectProductRequest
{
    NSString *url = @"Products/GetFavoriteProducts";
    [JNetManager getCollectProductRequedtUrl:url
                                     success:^(id response) {
                                         if (response) {
                                             self.productDataArr = response;
                                             if (self.productDataArr.count > 0) {
                                                 UIView *emptyView = self.dataEmptyViewArr[0];
                                                 emptyView.hidden = YES;
                                                 
                                                 _collectVeiw.dataArr = self.productDataArr;
                                             }
                                             else
                                             {
                                                 UIView *emptyView = self.dataEmptyViewArr[0];
                                                 emptyView.hidden = NO;
                                                 _collectVeiw.hidden = YES;
                                             }
                                             
                                             
                                         }
                                     } failure:^(NSString *err) {
                                         [SVProgressHUD showErrorWithStatus:err];
                                     }];
}

- (void)loadMyCollectShopRequest
{
    NSString *url = @"Vendor/GetCollectVendors";
    [JNetManager getCollectShopRequedtUrl:url
                                  success:^(id response) {
                                      if (response) {
                                          self.stopDataArr = response;
                                          if (self.stopDataArr.count > 0) {
                                              UIView *emptyView = self.dataEmptyViewArr[1];
                                              emptyView.hidden = YES;
                                              
                                              _collectShopVeiw.dataArr = self.stopDataArr;
                                          }
                                          else
                                          {
                                              UIView *emptyView = self.dataEmptyViewArr[1];
                                              emptyView.hidden = NO;
                                              _collectShopVeiw.hidden = YES;
                                          }
                                          
                                      }
                                  } failure:^(NSString *err) {
                                      [SVProgressHUD showErrorWithStatus:err];
                                  }];
}

/// 收藏店铺 delegate 回调
- (void)selCellShop:(NSIndexPath *)indexPath verdorModel:(JVendorModel *)model
{
    NSLog(@"选择收藏的店铺");
}

- (void)lineViewAnimation:(NSInteger)num
{
    
    [UIView animateWithDuration:0.3 animations:^{
        _lineView.frame = CGRectMake(60 + num * _btnWidth, _lineView.origin.y, _lineView.width, _lineView.height);
    }];
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger page = scrollView.contentOffset.x / scrollView.width;
    _lastBtnSel = page;
    [self lineViewAnimation:page];
    
}

- (void)orderBtnClick:(UIButton *)btn
{
    if (btn.tag - BtnTag == _lastBtnSel) {
        return;
    }
    
    [self lineViewAnimation:btn.tag - BtnTag];
    _lastBtnSel = btn.tag - BtnTag;
    [_scrollView setContentOffset:CGPointMake(_lastBtnSel * kWindowW, 0) animated:YES];
    
}

- (void)leftButtonClick:(UIButton *)leftButton
{
    [super leftButtonClick:leftButton];
    [self.navigationController popViewControllerAnimated:YES];
}


- (NSArray *)productDataArr
{
    if (_productDataArr == nil) {
        _productDataArr = [NSArray array];
    }
    return _productDataArr;
}

- (NSArray *)stopDataArr
{
    if (_stopDataArr == nil) {
        _stopDataArr = [NSArray array];
    }
    return _stopDataArr;
}

- (NSMutableArray *)dataEmptyViewArr
{
    if (_dataEmptyViewArr == nil) {
        _dataEmptyViewArr = [NSMutableArray array];
    }
    return _dataEmptyViewArr;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
